﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InternshipManagement.Models
{
    public class AnnouncementRepository
    {
        public InternshipEntities entities = new InternshipEntities();

        public IQueryable<Announcement> FindAllAnnouncements()
        {
            return entities.Announcements.Where(a => a.ReceiverID == null && a.Status != "P").OrderByDescending(a => a.SentDate);
        }

        public Announcement GetAnnouncement(int id)
        {
            return entities.Announcements.FirstOrDefault(a => a.ID == id);
        }

        public IQueryable<Announcement> GetReceivedAnnouncements(int UserType, int UserID,int? CourseID)
        {
            if (UserType == 4)//TODO
            {
                return entities.Announcements.Where(a => (a.IsStudentReceiving == true || a.ReceiverID == UserID)
                                                   && a.Status != "P" 
                                                   && (a.SentDate < DateTime.Now && a.CourseID == CourseID)).OrderByDescending(a => a.SentDate);
            }
            else if (UserType == 3)
            {
                return entities.Announcements.Where(a => (a.IsSecretaryReceiving == true || a.ReceiverID == UserID)
                                                   && a.Status != "P").OrderByDescending(a => a.SentDate);
            }
            else if (UserType == 2)
            {
                return entities.Announcements.Where(a => (a.IsCompanyReceiving == true || a.ReceiverID == UserID)
                                                   && a.Status != "P").OrderByDescending(a => a.SentDate);
            }
            else
            {
                return entities.Announcements.Where(a => (a.ReceiverID == UserID || (a.IsCompanyReceiving == false && a.IsSecretaryReceiving == false && a.IsStudentReceiving == false && a.ReceiverID == null))
                                                   && a.Status != "P").OrderByDescending(a => a.SentDate);
            }


        }

        public IQueryable<int> GetReadAnnouncements(int UserID)
        {
            return entities.ReadAnnouncements.Where(a => a.UserID == UserID).OrderByDescending(a => a.AnnouncementID).Select(a => a.AnnouncementID);
        }

        public IQueryable<int> GetUnreadAnnouncements(int UserType, int UserID, int? CourseID)
        {
            return from a in GetReceivedAnnouncements(UserType, UserID,CourseID)
                   join r in entities.ReadAnnouncements on new { a.ID, UserID } equals new { ID = r.AnnouncementID, r.UserID } into JoinedAnn
                   from r in JoinedAnn.DefaultIfEmpty()
                   where r.AnnouncementID == null
                   select a.ID;

        }

        public bool ReadAnnouncementExist(int AnnouncementID, int UserID)
        {
            return entities.ReadAnnouncements.FirstOrDefault(a => a.UserID == UserID && a.AnnouncementID == AnnouncementID) != null;
        }

        //
        // Insert/Delete Methods
        public void AddAnnouncement(Announcement announcement)
        {
            entities.Announcements.AddObject(announcement);
        }
        public void DeleteAnnouncement(Announcement announcement)
        {
            entities.Announcements.DeleteObject(announcement);
        }

        public void AddReadAnnouncement(ReadAnnouncement readannouncement)
        {
            entities.ReadAnnouncements.AddObject(readannouncement);
        }

        //
        // Persistence
        public void Save()
        {
            entities.SaveChanges();
        }
    }
}